//
//  JXAddressDetailViewController.m
//  JXAddressApp
//
//  Created by huawei.zheng on 2019/1/22.
//  Copyright © 2019 zhhw_home. All rights reserved.
//

#import "JXAddressDetailViewController.h"
#import "JXPOIDetailView.h"
#import "EgovaNetworking.h"
#import "QMUITips.h"
#import "JXMeAddAddressViewController.h"

#import <QMUIKit/QMUICommonDefines.h>

@interface JXAddressDetailViewController ()
<JXPOIDetailViewDelegate>

@property (nonatomic, strong)   JXPOIDetailView     *detailView;

@property (nonatomic, strong)   NSDictionary        *detailInfo;


@property (nonatomic, strong)   NSArray             *images;

@end

@implementation JXAddressDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = _info[@"name"];
    
    _detailView = [[JXPOIDetailView alloc] initWithFrame:CGRectMake(0, IS_NOTCHED_SCREEN?88:64, SCREEN_WIDTH, SCREEN_HEIGHT-(IS_NOTCHED_SCREEN?88:64)) flag:1];
    
    [self.view addSubview:_detailView];
    
    _detailView.delegate = self;
    
    [QMUITips showLoadingInView:self.view];
    
    [self searchDetail:_info[@"code"] name:_info[@"name"]];
}

- (void)searchDetail:(NSString*)code name:(NSString*)name
{
    NSDictionary *params = @{@"code":code,
                             @"stype": @(1),
                             @"searchname": name
                             };
    
    NSString *url = @"http://www.wegis.net/demo/home/baseinfo/getplacebycode";
    [EgovaNetworking getWithUrl:url cache:NO params:params completed:^(NSInteger code, id data, NSString *msg) {
        if (code==0 && data) {
            [QMUITips hideAllTipsInView:self.view];
            
            self.detailInfo = data;
            
            [self .detailView updateInfo:data];
        }
        else {
            [QMUITips showError:@"请求失败，请稍后重试..." inView:self.view hideAfterDelay:0.8];
        }
    }];
    
    [self searchImages:code];
}

- (void)searchImages:(NSString*)code
{
    NSDictionary *params = @{@"code":code,
                             @"type": @(5)
                             };
    
    NSString *url = @"http://www.wegis.net/demo/home/baseinfo/getplacemedia";
    [EgovaNetworking getWithUrl:url cache:NO params:params completed:^(NSInteger code, id data, NSString *msg) {
        if (code==0 && data) {
            self.images = data[@"media"];
            [self.detailView updateImages:self.images];
        }
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)editorAction:(NSDictionary *)item
{
    JXMeAddAddressViewController *vc = [JXMeAddAddressViewController new];
    vc.item = _detailInfo[@"place"];
    
    [self.navigationController pushViewController:vc animated:YES];
}



@end
